/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.base.dao.StoreSelfCareDao;
import com.chlitina.store.modules.base.entity.StoreSelfCare;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 店面自营护理Service
 * @author liujiang
 * @version 2013-11-02
 */
@Component
@Transactional(readOnly = true)
public class StoreSelfCareService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreSelfCareService.class);
	
	@Autowired
	private StoreSelfCareDao storeSelfCareDao;
	
	public StoreSelfCare get(Long id) {
		return storeSelfCareDao.findOne(id);
	}
	
	public Page<StoreSelfCare> find(Page<StoreSelfCare> page, String careName) {
		Store store = StoreUtils.getStore();
		DetachedCriteria dc = storeSelfCareDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(careName)){
			dc.add(Restrictions.like("name", "%"+careName+"%"));
		}
		dc.add(Restrictions.like("store", store));
		dc.addOrder(Order.asc("id"));
		return storeSelfCareDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreSelfCare storeSelfCare) {
		storeSelfCareDao.save(storeSelfCare);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		storeSelfCareDao.delete(id);
	}
	
	//获取当前记录排第几位
	public int getIndexById(Long currentId) {
		Store store = StoreUtils.getStore();
		DetachedCriteria dc = storeSelfCareDao.createDetachedCriteria();
		dc.add(Restrictions.like("store", store));
		dc.add(Restrictions.le("id", currentId));
		dc.addOrder(Order.asc("id"));
		return (int)storeSelfCareDao.count(dc);
	}
}
